package com.itheima.factory;

/**
 * Description:
 *
 * @author codermao
 * @date 2023/11/3 11:32
 */

import com.itheima.dao.AccountDao;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.context.annotation.Bean;

import java.io.IOException;
import java.io.InputStream;

/**
 * SqlSessionFactoryBuilder --> SqlSessionFactory --> SqlSession --> AccountDao（最重要）
 */
//@Component
public class MyBatisFactory_quit {

    //将SqlSessionFactoryBuilder放到ioc容器中
    @Bean
    public static SqlSessionFactoryBuilder getBuilder(){
        return new SqlSessionFactoryBuilder();
    }

    //将SqlSessionFactory放到ioc容器中
    @Bean
    public static SqlSessionFactory getFactory(SqlSessionFactoryBuilder builder) throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        return builder.build(inputStream);
    }

    @Bean
    public static SqlSession getSession(SqlSessionFactory factory){
        return factory.openSession(true);
    }

    @Bean
    public static AccountDao getDao(SqlSession session){
        return session.getMapper(AccountDao.class);
    }
}
